package com.coffee.daili.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import com.coffee.admin.service.supplier.bean.Supplier;
import com.coffee.admin.service.supplier.dao.SupplierDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.daili.api.query.DailiSkuQuery;
import com.coffee.daili.api.req.InsertSkuReq;
import com.coffee.daili.api.result.DailiSkuResult;
import com.coffee.daili.api.service.DailiSkuService;
import com.coffee.daili.service.bean.DailiSku;
import com.coffee.daili.service.dao.DailiSkuDao;
import com.coffee.sku.api.sku.query.SkuSalesInformationQuery;
import com.coffee.sku.api.sku.result.SkuSalesInformationResult;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.bean.SkuSalesInformation;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import com.coffee.sku.service.sku.dao.SkuSalesInformationDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class DailiSkuServiceImpl implements DailiSkuService {
    @Resource
    private DailiSkuDao dailiSkuDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private SkuSalesInformationDao skuSalesInformationDao;
    @Resource
    private SupplierDao supplierDao;

    @Override
    public Page<DailiSkuResult> getPager(DailiSkuQuery param, PagerInfo pr) {
        Page<DailiSkuResult> pager = new Page<DailiSkuResult>(pr.getStart(), pr.getPageSize());
        Integer count = dailiSkuDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<DailiSku> list = dailiSkuDao.page(param, pr.getStart(), pr.getPageSize());
            List<DailiSkuResult> dailiSkuResults = DTOUtils.convertList(list, DailiSkuResult.class);
            for (DailiSkuResult dailiSkuResult : dailiSkuResults) {
                if (dailiSkuResult.getCategoryId() != null) {
                    SkuCategory skuCategory = skuCategoryDao.findById(dailiSkuResult.getCategoryId());
                    if (skuCategory != null) {
                        dailiSkuResult.setCategoryName(skuCategory.getName());
                    }
                }
                SkuSalesInformationQuery skuSalesInformationQuery = new SkuSalesInformationQuery();
                skuSalesInformationQuery.setSkuId(dailiSkuResult.getSkuId());
                List<SkuSalesInformation> skuSalesInformations = skuSalesInformationDao.list(skuSalesInformationQuery);
                dailiSkuResult.setSkuSalesInformationResults(DTOUtils.convertList(skuSalesInformations, SkuSalesInformationResult.class));
                if (dailiSkuResult.getSupplierId() != null) {
                    Supplier supplier = supplierDao.findById(dailiSkuResult.getSupplierId());
                    if (supplier != null)
                        dailiSkuResult.setSupplierName(supplier.getName());
                }
            }
            pager.setData(dailiSkuResults);
        }
        return pager;
    }

    @Override
    public Integer count(DailiSkuQuery param) {
        Integer count = dailiSkuDao.count(param);
        return count;
    }

    @Override
    public DailiSkuResult getDetail(Integer id) {
        DailiSku item = dailiSkuDao.findById(id);
        if (item == null) {
            return null;
        }
        DailiSkuResult result = new DailiSkuResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<DailiSkuResult> getList(DailiSkuQuery param, PagerInfo pr) {
        List<DailiSku> list = dailiSkuDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, DailiSkuResult.class);
    }

    @Override
    public List<DailiSkuResult> getList(DailiSkuQuery param) {
        List<DailiSku> list = dailiSkuDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, DailiSkuResult.class);
    }

    @Override
    public List<DailiSkuResult> getList() {
        DailiSkuQuery param = new DailiSkuQuery();
        List<DailiSku> list = dailiSkuDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, DailiSkuResult.class);
    }


    @Override
    public void delete(Integer id) {
        dailiSkuDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(DailiSkuResult result) throws BusinessException {
        DailiSku item = new DailiSku();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            dailiSkuDao.insert(item);
        } else {
            DailiSku tmp = dailiSkuDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            dailiSkuDao.updateById(item);
        }
    }

    @Override
    public void insert(InsertSkuReq req) throws BusinessException {
        List<Integer> skuIdsBySupplierId = dailiSkuDao.findSkuIdsBySupplierId(req.getSupplierId());
        Collection subtract = CollectionUtils.subtract(req.getSkuIds(), skuIdsBySupplierId);
        List<Integer> subtractList = new ArrayList<>(subtract);
        List<DailiSku> list = new ArrayList<>();
        if (subtractList.size() > 0) {
            for (Integer integer : subtractList) {
                DailiSku dailiSku = new DailiSku();
                dailiSku.setSupplierId(req.getSupplierId());
                dailiSku.setSkuId(integer);
                dailiSku.setProportion(req.getProportion());
                dailiSku.setCreateTime(new Date());
                list.add(dailiSku);
            }
            dailiSkuDao.insertBatch(list);
        }
    }

    @Override
    public Map<Integer, DailiSkuResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, DailiSkuResult> map = new HashMap<Integer, DailiSkuResult>();
        List<DailiSku> list = dailiSkuDao.findByIds(StringUtil.setToList(idSet));
        List<DailiSkuResult> resultList = DTOUtils.convertList(list, DailiSkuResult.class);
        for (DailiSkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, DailiSkuResult> getItemMap() {
        Map<Integer, DailiSkuResult> map = new HashMap<Integer, DailiSkuResult>();
        DailiSkuQuery param = new DailiSkuQuery();
        List<DailiSku> list = dailiSkuDao.page(param, 0, Integer.MAX_VALUE);
        List<DailiSkuResult> resultList = DTOUtils.convertList(list, DailiSkuResult.class);
        for (DailiSkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
